package com.csu.device.web;

import com.alibaba.fastjson.JSONObject;
import com.csu.analysis.service.GpsService;
import com.csu.analysis.service.MattressService;
import com.csu.device.dao.BehaviorDao;
import com.csu.device.entity.Behavior;
import com.csu.device.service.HardWareService;
import com.csu.push.WebSocketPush;
import com.csu.util.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;


/**
 * 数据获取接口
 */
@Controller
@RequestMapping("/device")
public class DataController {

    private Logger logger = LoggerFactory.getLogger(DataController.class);

    @Autowired
    private HardWareService hardWareService;

    @Autowired
    private MattressService mattressService;

    @Autowired
    private GpsService gpsService;

    @Autowired
    private BehaviorDao behaviorDao;

    @GetMapping("")
    String index() {
        return "/device/example";
    }


    /**
     * 硬件设备post
     * 从网关过来的数据
     * @param jsonParam
     * @return
     */
    @RequestMapping(value = "postData", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public R postData(@RequestBody JSONObject jsonParam, HttpServletRequest request) throws Exception {

        logger.info("data:  "+jsonParam);
        hardWareService.deviceProsson(jsonParam);
        return R.ok();
    }

    /**
     * 床垫数据
     *
     * @param jsonParam
     * @return
     */
    @RequestMapping(value = "mattress", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public R mattress(@RequestBody JSONObject jsonParam) {
        logger.info("data:  "+jsonParam);
        mattressService.proxyMattress(jsonParam);
        return R.ok();
    }


    /**
     * gps
     *
     * @param jsonParam
     * @return
     */
    @RequestMapping(value = "gps", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public R gps(@RequestBody JSONObject jsonParam) {
        logger.info("data:  "+jsonParam);
        gpsService.gps(jsonParam);
        return R.ok();
    }


    /**
     * 测试postIFID
     *
     * @param jsonParam
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "test", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public R test(@RequestBody JSONObject jsonParam) throws Exception {

        Behavior behavior = new Behavior();
        behavior.setStartTime(new Date());
        behavior.setLevel(3);
        behavior.setDeviceName("传感器异常");
        behavior.setSceneName("单人间");
        behavior.setFamilyName("刘志博之家");
        behavior.setFamilyUserName("卡卡西");
        behavior.setRemark("温度传感器异常");
        behavior.setAnomalies(1);
        WebSocketPush.errorPush(behavior);
        behaviorDao.addBehavior(behavior);
        return R.ok();
    }


}
